//
//  YHMeOrderDViewController.m
//  YHApp
//
//  Created by -- on 2021/8/9.
//

#import "HAOrderDVC.h"
#import "HAOrderMo.h"
#import "HAAddressMo.h"
#import "HAShoppingMallBuyAddressCell.h"
#import "HAOrderListCell.h"
#import "HAShoppingMallBuyOtherCell.h"

@interface HAOrderDVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;
/***/
@property (nonatomic, strong) HAOrderMo *model;

@end

@implementation HAOrderDVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.baseTLabel.text = @"订单详情";
    
    [self setupUI];
    [self loadNewData];
    
    // Do any additional setup after loading the view.
}

-(void)loadNewData {
    
    NSDictionary *dict = @{
        @"order_id":self.order_id?:@"",
    };
    SYRequest *request1 = [[SYRequest alloc]init];
    request1.requestUrl = [NSString stringWithFormat:@"order/detail"];
    request1.requestMethod = SYRequestMethodGET;
    request1.parms = dict;
    [MBProgressHUD showLoading:self.view];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request1] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request1) {
            
            [MBProgressHUD hideHUDForView:self.view];
            
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                [self.navigationController popViewControllerAnimated:YES];
                return;
            }
            if (dataModel.code == 1) {
                self.model = [HAOrderMo mj_objectWithKeyValues:dataModel.data];
                
                if (self.model) {
                    [self.dataArray removeAllObjects];
                    [self.dataArray addObject:@"地址"];
                    [self.dataArray addObject:@"信息"];
                    [self.dataArray addObject:@"下单时间"];
                    [self.dataArray addObject:@"支付方式"];
                    [self.dataArray addObject:@"配送方式"];
                    [self.dataArray addObject:@"支付状态"];
                    [self.dataArray addObject:@"实际付款"];
                }

            }else{
                [self.navigationController popViewControllerAnimated:YES];
                [MBProgressHUD showError:dataModel.msg];
            }
            
            [self.tableView reloadData];
        }
    }];
    
}

-(void)setupUI {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(app_topH+44);
        make.bottom.mas_equalTo(0);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"地址"]) {
        return 80+15;
    }
    if ([title isEqualToString:@"信息"]) {
        return 92+32+5;
    }
    if ([title isEqualToString:@"下单时间"] || [title isEqualToString:@"支付方式"]||[title isEqualToString:@"配送方式"]||[title isEqualToString:@"支付状态"] || [title isEqualToString:@"实际付款"]) {
        return 54;
    }
    return 0;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
//    WS(weakSelf)
    if ([title isEqualToString:@"地址"]) {
        HAShoppingMallBuyAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HAShoppingMallBuyAddressCellID"];
        if (!cell) {
            cell = [[HAShoppingMallBuyAddressCell alloc]initWithStyle:0 reuseIdentifier:@"HAShoppingMallBuyAddressCellID"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.model.address;
        return cell;
    }
    if ([title isEqualToString:@"信息"]) {
        HAOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMeOrderListCellDID"];
        if (!cell) {
            cell = [[HAOrderListCell alloc]initWithStyle:0 reuseIdentifier:@"YHMeOrderListCellDID"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.isD = YES;
        cell.model = self.model;
        return cell;
    }
    if ([title isEqualToString:@"下单时间"] || [title isEqualToString:@"支付方式"]||[title isEqualToString:@"配送方式"]||[title isEqualToString:@"支付状态"] || [title isEqualToString:@"实际付款"]) {
        HAShoppingMallBuyOtherCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHShoppingMallBuyOtherCellDID"];
        if (!cell) {
            cell = [[HAShoppingMallBuyOtherCell alloc]initWithStyle:0 reuseIdentifier:@"YHShoppingMallBuyOtherCellDID"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titText = title;
        if ([title isEqualToString:@"下单时间"]) {
            NSString *timeText = [NSString stringWithFormat:@"%@",[NSString timeStampToString:self.model.created_at Formatter:@"yyyy-MM-dd HH:mm"]];
            cell.conText = timeText;
        }
        if ([title isEqualToString:@"支付方式"]) {
            cell.conText = self.model.payment_name;
        }
        if ([title isEqualToString:@"配送方式"]) {
            cell.conText = self.model.shipping_name;
        }
        if ([title isEqualToString:@"支付状态"]) {
            cell.conText = self.model.sate;
        }
        if ([title isEqualToString:@"实际付款"]) {
            cell.conText = [NSString stringWithFormat:@"￥%@",self.model.total];
        }

        return cell;
    }
    
    
    return [UITableViewCell new];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor khaBGColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array ];
    }
    return _dataArray;
}



@end
